package club.sanmubird.springboot_mysqlAndMongodb.mongodb.config;

import com.mongodb.MongoClient;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: springboot-simpleMongDb
 * @description: dataSourceConfig
 * @author: sam
 * @create: 2018-07-18 23:22
 * @update: 2018-07-18 23:22
 * @update: Sam
 **/
@Configuration
public class MongoDataSourceConfig {
	// Inject an instance of Spring-Boot MongoProperties
	@Autowired
	private MongoProperties mongoProperties;

	private Morphia morphia() {
		final Morphia morphia = new Morphia();
		// tell Morphia where to find your classes
		// can be called multiple times with different packages or classes
		morphia.mapPackage("club.sanmubird.springbootsimpleMongDb.entity");

		return morphia;
	}

	@Bean
	public Datastore datastore(MongoClient mongoClient) {
		// create the Datastore connecting to the default port on the local host
		final Datastore datastore = morphia().createDatastore(mongoClient, mongoProperties.getDatabase());
		datastore.ensureIndexes();

		return datastore;
	}
}


